package com.bt.lms.service.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.CourseInfo;
import com.bt.lms.entity.page.Page;
import com.bt.lms.bo.QueryCourseCondition;
import com.bt.lms.dao.servise.CourseInfoDao;
import com.bt.lms.dao.servise.impl.CourseInfoDaoImpl;
import com.bt.lms.service.CourseInfoService;

/**
 * 课程信息服务类
 *
 * @author 李鹏
 */
public class CourseInfoServiceImpl implements CourseInfoService {

    @Override
    public boolean add(CourseInfo courseInfo) throws Exception {
        //sql语句
        String sql = "insert into course_info(code,course_name,english_name,study_center,apply_level,versions) values(?,?,?,?,?,?)";
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        courseInfoDao.insert(sql, courseInfo);

        return true;
    }

    @Override
    public CourseInfo detail(Long id) throws Exception {

        String sql = "select * from course_info where id = ?";
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        CourseInfo courseInfo = courseInfoDao.select(sql, id);

        return courseInfo;
    }

    @Override
    public boolean edit(Long id, CourseInfo courseInfo) throws Exception {

        //sql语句
        String sql = "update course_info set code = ?,course_name = ?,english_name = ?,study_center = ?,apply_level = ?,versions = ? where id = ?";
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        courseInfoDao.update(sql, id, courseInfo);

        return false;
    }

    @Override
    public boolean delete(Long id) throws Exception {

        //sql语句
        String sql = "update course_info set deleted = " + BOConstant.DELETED_TRUE + " where id = ?";
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        courseInfoDao.delete(sql, id);

        return false;
    }

    @Override
    public Page<CourseInfo> query(Page<CourseInfo> page) {

        //课程信息sql
        String sql = "select * from course_info limit ?,?"; //第几页，几条数据

        //课程信息dao
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        Page<CourseInfo> courseInfoPage = courseInfoDao.pageQuery(sql, page);

        return courseInfoPage;
    }

    @Override
    public boolean start(Long id) throws Exception {

        //sql语句
        String sql = "update course_info set status = " + BOConstant.COURSE_INFO_STATUS_START + " where id = ?";
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        courseInfoDao.updateStart(sql, id);

        return false;
    }

    @Override
    public boolean stop(Long id) throws Exception {

        //sql语句
        String sql = "update course_info set status = " + BOConstant.COURSE_INFO_STATUS_STOP + " where id = ?";
        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        courseInfoDao.updateStop(sql, id);

        return false;
    }

    @Override
    public boolean addAndStart(CourseInfo courseInfo, Long id) throws Exception {

        //添加sql语句
        String addSql = "insert into course_info(code,course_name,english_name,study_center,apply_level,versions) values(?,?,?,?,?,?)";

        //更新sql语
        String updateSql = "update course_info set status = " + BOConstant.COURSE_INFO_STATUS_START + " where id = ?";

        CourseInfoDao courseInfoDao = new CourseInfoDaoImpl();
        courseInfoDao.addAndUpdateStart(addSql, updateSql, courseInfo, id);

        return false;
    }
}
